package com.yst.webplugin.annotation;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.ServerHttpResponse;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

public class Response implements ServerHttpResponse {
    private final HttpServletResponse httpServletResponse;
    private final HttpHeaders headers=new HttpHeaders();
    public Response(HttpServletResponse httpServletResponse){
        this.httpServletResponse=httpServletResponse;
    }
    @Override
    public void setStatusCode(HttpStatus status) {
        httpServletResponse.setStatus(status.value());
    }

    @Override
    public void flush() throws IOException {
        httpServletResponse.flushBuffer();
    }

    @Override
    public void close() {
        try {
            httpServletResponse.getWriter().close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public OutputStream getBody() throws IOException {
        return httpServletResponse.getOutputStream();
    }

    @Override
    public HttpHeaders getHeaders() {
        return headers;
    }
}
